package com.lifeverse.controller;

import com.lifeverse.common.ApiResponse;
import com.lifeverse.dto.LifeEntityRelationshipCreateDTO;
import com.lifeverse.dto.LifeEntityRelationshipDTO;
import com.lifeverse.dto.LifeEntityRelationshipUpdateDTO;
import com.lifeverse.entity.LifeEntityRelationship;
import com.lifeverse.service.LifeEntityRelationshipService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 生命体关系管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/relationships")
@RequiredArgsConstructor
@Tag(name = "生命体关系管理", description = "生命体关系的创建、查询、更新和删除")
public class LifeEntityRelationshipController {
    
    private final LifeEntityRelationshipService relationshipService;
    
    @Operation(summary = "创建生命体关系", description = "在两个生命体之间建立关系")
    @PostMapping
    public ApiResponse<LifeEntityRelationshipDTO> createRelationship(
            @Valid @RequestBody LifeEntityRelationshipCreateDTO createDTO) {
        log.info("创建生命体关系请求: {}", createDTO);
        LifeEntityRelationshipDTO result = relationshipService.createRelationship(createDTO);
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "更新生命体关系", description = "更新指定的生命体关系信息")
    @PutMapping("/{relationshipId}")
    public ApiResponse<LifeEntityRelationshipDTO> updateRelationship(
            @Parameter(description = "关系ID") @PathVariable Long relationshipId,
            @Valid @RequestBody LifeEntityRelationshipUpdateDTO updateDTO) {
        log.info("更新生命体关系请求: id={}, data={}", relationshipId, updateDTO);
        LifeEntityRelationshipDTO result = relationshipService.updateRelationship(relationshipId, updateDTO);
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "删除生命体关系", description = "删除指定的生命体关系")
    @DeleteMapping("/{relationshipId}")
    public ApiResponse<Void> deleteRelationship(
            @Parameter(description = "关系ID") @PathVariable Long relationshipId) {
        log.info("删除生命体关系请求: id={}", relationshipId);
        relationshipService.deleteRelationship(relationshipId);
        return ApiResponse.success();
    }
    
    @Operation(summary = "根据ID查询关系", description = "根据关系ID查询关系详情")
    @GetMapping("/{relationshipId}")
    public ApiResponse<LifeEntityRelationshipDTO> getRelationshipById(
            @Parameter(description = "关系ID") @PathVariable Long relationshipId) {
        LifeEntityRelationshipDTO result = relationshipService.getRelationshipById(relationshipId);
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "查询生命体的所有关系", description = "查询指定生命体的所有关系")
    @GetMapping("/entity/{entityId}")
    public ApiResponse<List<LifeEntityRelationshipDTO>> getRelationshipsByEntity(
            @Parameter(description = "生命体ID") @PathVariable Long entityId) {
        List<LifeEntityRelationshipDTO> result = relationshipService.getRelationshipsByEntity(entityId);
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "查询生命体的活跃关系", description = "查询指定生命体的活跃关系")
    @GetMapping("/entity/{entityId}/active")
    public ApiResponse<List<LifeEntityRelationshipDTO>> getActiveRelationshipsByEntity(
            @Parameter(description = "生命体ID") @PathVariable Long entityId) {
        List<LifeEntityRelationshipDTO> result = relationshipService.getActiveRelationshipsByEntity(entityId);
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "根据关系类型查询", description = "根据关系类型查询所有关系")
    @GetMapping("/type/{type}")
    public ApiResponse<List<LifeEntityRelationshipDTO>> getRelationshipsByType(
            @Parameter(description = "关系类型") @PathVariable LifeEntityRelationship.RelationshipType type) {
        List<LifeEntityRelationshipDTO> result = relationshipService.getRelationshipsByType(type);
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "查询强关系", description = "查询所有强关系（强度大于70）")
    @GetMapping("/strong")
    public ApiResponse<List<LifeEntityRelationshipDTO>> getStrongRelationships() {
        List<LifeEntityRelationshipDTO> result = relationshipService.getStrongRelationships();
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "查询高信任度关系", description = "查询所有高信任度关系（信任度大于80）")
    @GetMapping("/high-trust")
    public ApiResponse<List<LifeEntityRelationshipDTO>> getHighTrustRelationships() {
        List<LifeEntityRelationshipDTO> result = relationshipService.getHighTrustRelationships();
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "分页查询关系", description = "根据关系状态分页查询关系")
    @GetMapping("/status/{status}")
    public ApiResponse<Page<LifeEntityRelationshipDTO>> getRelationshipsByStatus(
            @Parameter(description = "关系状态") @PathVariable LifeEntityRelationship.RelationshipStatus status,
            @PageableDefault(size = 20) Pageable pageable) {
        Page<LifeEntityRelationshipDTO> result = relationshipService.getRelationshipsByStatus(status, pageable);
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "加强关系", description = "增加指定关系的强度")
    @PostMapping("/{relationshipId}/strengthen")
    public ApiResponse<LifeEntityRelationshipDTO> strengthenRelationship(
            @Parameter(description = "关系ID") @PathVariable Long relationshipId,
            @Parameter(description = "增加的强度值") @RequestParam BigDecimal amount) {
        log.info("加强关系请求: id={}, amount={}", relationshipId, amount);
        LifeEntityRelationshipDTO result = relationshipService.strengthenRelationship(relationshipId, amount);
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "减弱关系", description = "减少指定关系的强度")
    @PostMapping("/{relationshipId}/weaken")
    public ApiResponse<LifeEntityRelationshipDTO> weakenRelationship(
            @Parameter(description = "关系ID") @PathVariable Long relationshipId,
            @Parameter(description = "减少的强度值") @RequestParam BigDecimal amount) {
        log.info("减弱关系请求: id={}, amount={}", relationshipId, amount);
        LifeEntityRelationshipDTO result = relationshipService.weakenRelationship(relationshipId, amount);
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "终止关系", description = "终止指定的生命体关系")
    @PostMapping("/{relationshipId}/terminate")
    public ApiResponse<LifeEntityRelationshipDTO> terminateRelationship(
            @Parameter(description = "关系ID") @PathVariable Long relationshipId) {
        log.info("终止关系请求: id={}", relationshipId);
        LifeEntityRelationshipDTO result = relationshipService.terminateRelationship(relationshipId);
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "更新互动时间", description = "更新关系的最后互动时间")
    @PostMapping("/{relationshipId}/interaction")
    public ApiResponse<Void> updateInteractionTime(
            @Parameter(description = "关系ID") @PathVariable Long relationshipId) {
        relationshipService.updateInteractionTime(relationshipId);
        return ApiResponse.success();
    }
    
    @Operation(summary = "查询需要维护的关系", description = "查询质量低或信任度低的关系")
    @GetMapping("/maintenance-needed")
    public ApiResponse<List<LifeEntityRelationshipDTO>> getRelationshipsNeedingMaintenance() {
        List<LifeEntityRelationshipDTO> result = relationshipService.getRelationshipsNeedingMaintenance();
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "查询长时间未互动的关系", description = "查询指定天数内未互动的关系")
    @GetMapping("/inactive")
    public ApiResponse<List<LifeEntityRelationshipDTO>> getInactiveRelationships(
            @Parameter(description = "天数阈值") @RequestParam(defaultValue = "30") int daysThreshold) {
        List<LifeEntityRelationshipDTO> result = relationshipService.getInactiveRelationships(daysThreshold);
        return ApiResponse.success(result);
    }
    
    @Operation(summary = "统计生命体关系数量", description = "统计指定生命体的关系数量")
    @GetMapping("/entity/{entityId}/count")
    public ApiResponse<Long> countRelationshipsByEntity(
            @Parameter(description = "生命体ID") @PathVariable Long entityId) {
        long count = relationshipService.countRelationshipsByEntity(entityId);
        return ApiResponse.success(count);
    }
    
    @Operation(summary = "统计关系类型数量", description = "统计指定类型的关系数量")
    @GetMapping("/type/{type}/count")
    public ApiResponse<Long> countRelationshipsByType(
            @Parameter(description = "关系类型") @PathVariable LifeEntityRelationship.RelationshipType type) {
        long count = relationshipService.countRelationshipsByType(type);
        return ApiResponse.success(count);
    }
}